/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logical_spaces.business;

import Entity.TableSpace;
import Entity.TableSpaceInfo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import logical_spaces.DAO.TableSpaceDAO;


/**
 *
 * @author JuanFelipe
 */
public class LogicalSpaceBusiness {
    
    private ArrayList<TableSpace> tableSpace ;
    private ArrayList<TableSpaceInfo> tableSpaceInfo ;
    public Connection conn;

    public LogicalSpaceBusiness( Connection currentConnection ){
        this.tableSpace = new ArrayList<>();
        conn = currentConnection;
    }

    public ArrayList<TableSpace> getTableSpace() {
        return tableSpace;
    }

    public void setTableSpace(ArrayList<TableSpace> tableSpace) {
        this.tableSpace = tableSpace;
    }
    
    
    public ArrayList<TableSpace> chargePermanentTableSpaceData() throws SQLException{
        
        String sql="SELECT TABLESPACE_NAME \"TABLESPACE\",\n" +
                    "   INITIAL_EXTENT \"INITIAL_EXT\",\n" +
                    "   NEXT_EXTENT \"NEXT_EXT\",\n" +
                    "   MIN_EXTENTS \"MIN_EXT\",\n" +
                    "   MAX_EXTENTS \"MAX_EXT\",\n" +
                    "   PCT_INCREASE\n" +
                    "   FROM DBA_TABLESPACES\n" +
                    "   WHERE CONTENTS = 'PERMANENT'";
        try {

            ResultSet rs = new TableSpaceDAO(conn).executeStatement(sql);
            while (rs.next()) {
                ArrayList<String> stringList = new ArrayList<>();

                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    stringList.add(rs.getString(i));
                }
                tableSpace.add(new TableSpace(stringList.get(0), (stringList.get(1) != null)
                        ? (Integer.parseInt(stringList.get(1))) : -1, (stringList.get(2) != null)
                        ? (Integer.parseInt(stringList.get(2))) : -1, (stringList.get(3) != null)
                        ? (Integer.parseInt(stringList.get(3))) : -1, (stringList.get(4) != null)
                        ? (Integer.parseInt(stringList.get(4))) : -1, (stringList.get(5) != null)
                        ? (Integer.parseInt(stringList.get(5))) : -1));
            }
        } catch (SQLException e) {
            System.out.println("->->Mensaje1: " + e.getSQLState());
            System.out.println("->->Stack1: " + e.getStackTrace());
            throw new SQLException(e.getMessage());
        } catch (Exception e) {
            System.out.println("->->Mensaje: " + e.getMessage());
            System.out.println("->->Stack: " + e.getClass());
            throw e;
        }
        return tableSpace;
    }
    
 public ArrayList<TableSpace> chargeTemporaryTableSpaceData() throws SQLException{
        
        String sql="SELECT TABLESPACE_NAME \"TABLESPACE\",\n" +
                    "   INITIAL_EXTENT \"INITIAL_EXT\",\n" +
                    "   NEXT_EXTENT \"NEXT_EXT\",\n" +
                    "   MIN_EXTENTS \"MIN_EXT\",\n" +
                    "   MAX_EXTENTS \"MAX_EXT\",\n" +
                    "   PCT_INCREASE\n" +
                    "   FROM DBA_TABLESPACES\n" +
                    "   WHERE CONTENTS = 'TEMPORARY'";
        try {

            ResultSet rs = new TableSpaceDAO(conn).executeStatement(sql);
            while (rs.next()) {
                ArrayList<String> stringList = new ArrayList<>();

                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    stringList.add(rs.getString(i));
                }
                tableSpace.add(new TableSpace(stringList.get(0), (stringList.get(1) != null)
                        ? (Integer.parseInt(stringList.get(1))) : -1, (stringList.get(2) != null)
                        ? (Integer.parseInt(stringList.get(2))) : -1, (stringList.get(3) != null)
                        ? (Integer.parseInt(stringList.get(3))) : -1, (stringList.get(4) != null)
                        ? (Integer.parseInt(stringList.get(4))) : -1, (stringList.get(5) != null)
                        ? (Integer.parseInt(stringList.get(5))) : -1));
            }
        } catch (SQLException e) {
            System.out.println("->->Mensaje1: " + e.getSQLState());
            System.out.println("->->Stack1: " + e.getStackTrace());
            throw new SQLException(e.getMessage());
        } catch (Exception e) {
            System.out.println("->->Mensaje: " + e.getMessage());
            System.out.println("->->Stack: " + e.getClass());
            throw e;
        }
        return tableSpace;
    }
 
 public ArrayList<TableSpace> chargeUndoTableSpaceData() throws SQLException{
        
        String sql="SELECT TABLESPACE_NAME \"TABLESPACE\",\n" +
                    "   INITIAL_EXTENT \"INITIAL_EXT\",\n" +
                    "   NEXT_EXTENT \"NEXT_EXT\",\n" +
                    "   MIN_EXTENTS \"MIN_EXT\",\n" +
                    "   MAX_EXTENTS \"MAX_EXT\",\n" +
                    "   PCT_INCREASE\n" +
                    "   FROM DBA_TABLESPACES\n" +
                    "   WHERE CONTENTS = 'UNDO'";
        try {

            ResultSet rs = new TableSpaceDAO(conn).executeStatement(sql);
            while (rs.next()) {
                ArrayList<String> stringList = new ArrayList<>();

                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    stringList.add(rs.getString(i));
                }
                tableSpace.add(new TableSpace(stringList.get(0), (stringList.get(1) != null)
                        ? (Integer.parseInt(stringList.get(1))) : -1, (stringList.get(2) != null)
                        ? (Integer.parseInt(stringList.get(2))) : -1, (stringList.get(3) != null)
                        ? (Integer.parseInt(stringList.get(3))) : -1, (stringList.get(4) != null)
                        ? (Integer.parseInt(stringList.get(4))) : -1, (stringList.get(5) != null)
                        ? (Integer.parseInt(stringList.get(5))) : -1));
            }
        } catch (SQLException e) {
            System.out.println("->->Mensaje1: " + e.getSQLState());
            System.out.println("->->Stack1: " + e.getStackTrace());
            throw new SQLException(e.getMessage());
        } catch (Exception e) {
            System.out.println("->->Mensaje: " + e.getMessage());
            System.out.println("->->Stack: " + e.getClass());
            throw e;
        }
        return tableSpace;
    }
  
}
